using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace Watchfire.DefectLogger
{
	/// <summary>
	/// Access to the registry for AppScan Extensions
	/// </summary>
	class PluginRegistry
	{
		/// <summary>
		/// Put all extension registry stuff under the Plugins subkey
		/// </summary>
		private static readonly string _regPlugins = "Plugins";
		

		/// <summary>
		/// Read a string value from the registry
		/// </summary>
		/// <param name="appScanRegistryPath">Path to AppScan registry</param>
		/// <param name="subkey">Subkey of the Plugins subkey</param>
		/// <param name="stringValue">Name of the string value to be retrieved</param>
		/// <returns>Value of the string value retrieved or null if it couldn't be found</returns>
		public static string Read(string appScanRegistryPath, string subkey, string stringValue)
		{
			// If the read fails, just don't load the settings
			string result = null;
			try
			{
				RegistryKey AppScanKey = Registry.CurrentUser.OpenSubKey(appScanRegistryPath);
				RegistryKey PluginsKey = AppScanKey.OpenSubKey(_regPlugins);
				if (PluginsKey != null)
				{
					RegistryKey DefectLoggerPluginKey = PluginsKey.OpenSubKey(subkey);
					if (DefectLoggerPluginKey != null)
					{
						result = DefectLoggerPluginKey.GetValue(stringValue).ToString();
						DefectLoggerPluginKey.Close();
					}
					PluginsKey.Close();
				}
				AppScanKey.Close();
			}
			catch (Exception ex)
			{
				// TODO
				Console.Out.Write(ex.ToString());
			}
			return result;
		}

		/// <summary>
		/// Write a string to the registry
		/// </summary>
		/// <param name="appScanRegistryPath">Path to AppScan registry</param>
		/// <param name="subkey">Subkey of the Plugins subkey</param>
		/// <param name="stringValue">Name of the string value to be written</param>
		/// <param name="stringValueValue">Value fo the string value to be written</param>
		public static void WriteSettings(string appScanRegistryPath, string subkey, string stringValue, string stringValueValue)
		{
			// Write shouldn't fail, but a plugin shouldn't mess up the whole product
			try
			{
				RegistryKey AppScanKey = Registry.CurrentUser.OpenSubKey(appScanRegistryPath, true);
				RegistryKey PluginsKey = AppScanKey.OpenSubKey(_regPlugins, true);
				if (PluginsKey == null)
					PluginsKey = AppScanKey.CreateSubKey(_regPlugins, RegistryKeyPermissionCheck.ReadWriteSubTree);

				RegistryKey DefectLoggerPluginKey = PluginsKey.OpenSubKey(subkey, true);
				if (DefectLoggerPluginKey == null)
					DefectLoggerPluginKey = PluginsKey.CreateSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, AppScanKey.GetAccessControl());

				DefectLoggerPluginKey.SetValue(stringValue, stringValueValue);

				DefectLoggerPluginKey.Close();
				PluginsKey.Close();
				AppScanKey.Close();
			}
			catch (Exception ex)
			{
				// TODO
				Console.Out.Write(ex.ToString());
			}
		}
	}
}
